const db = require('../db/index')

exports.addMovie = (req, res) => {
    const sql = 'select * from car where refid = ? and userid = ?'
    db.query(sql, [req.body.refid, req.user.id], (err, results) => {
        if (err) {
            return res.cc(err.message)
        }
        if (results.length === 0) {
            const sql = 'insert into car set ?'
            db.query(sql, { refid: req.body.refid, userid: req.user.id, price: req.body.price, moviename: req.body.moviename, posters: req.body.posters }, (err, results) => {
                // 执行 SQL 语句失败
                if (err) {
                    return res.cc(err.message)
                }
                res.send({
                    status: 200,
                    message: '成功'
                })
            })
        } else {
            res.send({
                status: 200,
                message: '成功'
            })
        }
    })
}

exports.getMovie = (req, res) => {
    const sql = 'select * from car where userid = ?'
    db.query(sql, req.user.id, (err, results) => {
        if (err) {
            return res.cc(err.message)
        }
        res.send({
            status: 200,
            message: '成功',
            data: results
        })
    })

}

exports.deleteMovie = (req, res) => {
    const sql = 'delete from car where id = ?'
    db.query(sql, req.params.id, (err, results) => {
        if (err) {
            return res.cc(err.message)
        }
        res.send({
            status: 200,
            message: '成功'
        })
    })
}